<?php
/**
 * Created by PhpStorm.
 * User: samsung
 * Date: 2018/8/12
 * Time: 9:59
 */

namespace app\index\controller;
use think\Db;


//单条语句查询
class Demo4
{
    public  function  conn1(){
        //Db::execute('insert into user (username,password,address,telphone) values(?,?,?,?)',['zhanghu','3','thinkphp','thinkphp']);
        //Db::execute('update user set username="thinkphp" where id=1 ');

        //Db::table('user')->where('id',0)->find();
        $res = Db::table('user')

            ->field(['id'=>'编号','username'=>'姓名'])
            //->field('id,username,telphone')
            //->where('id','>',1)
            ->find(1);
        dump (is_null($res) ? '没有找到！':$res);
    }
//多条语句查询
    public  function selectmore(){
        $res2 = Db::table('user')
            ->field('id,username,password')
            ->where([
                ['username','=','zhanghu'],
                ['password','>','1']
            ])
            ->select();
        if (empty($res2)){
            return '你所查询的数据不存在！';
        } else{
            foreach ($res2 as $row){
                dump($row);
            }
        }
        //dump($res2);
    }
    //单条插入
    //返回新增数量，失败返回false;
    public function insertone(){
        $data = [
            'username' => 'alonebar',
            'password' => '394373',
            'address' => '嵊州官河南路',
            'telphone' => '15068972211',
        ];
        //return Db::table('user')->insert($data);
        // INSERT INTO `user` (`username` , `password` , `address` , `telphone`) VALUES ('haohao' , '212' , '嵊州范二村' , '18959513272')

        //return Db::table('user')->insert($data,true);//只有数据库类型是mysql的时候才可以插入TRUE这个参数，其他的必定失败.
        //REPLACE INTO `user` (`username` , `password` , `address` , `telphone`) VALUES ('haohao' , '212' , '嵊州范二村' , '18959513272')

        //return Db::table('user')->Data($data)->insert();//Data()方法有过滤$data数据,这样子更加安全,但是insert()里面就没有true参数
        //INSERT INTO `user` (`username` , `password` , `address` , `telphone`) VALUES ('alonebar' , '394373' , '嵊州官河南路' , '15068972211')

        //return Db::table('user')->insertGetId($data);//insertGetId()实行两步操作，插入数据同时，返回插入的主键。
    }

    //多条数据插入
    public function insertmore(){
        $data = [
            ['username' => '张三','password' => 'allen0531','address'=>'嵊州信息港1','telphone'=>'17821009812'],
            ['username' => '张四','password' => 'allen0521','address'=>'嵊州信息港2','telphone'=>'17821009811'],
            ['username' => '王五','password' => 'allen0511','address'=>'嵊州信息港3','telphone'=>'17821009813'],
        ];
        return Db::table('user')->insertAll($data);
    }

    //更新操作
    public function update(){
        //更新操作，必须是有条件的才可以操作.
        return Db::table('user')
           // ->where('id',5)
           //->update(['username'=>'张虎']);
            ->update(['username' => '林若婵','id' => 7]);
    }

    public function detele(){
        return Db::table('user')
            ->delete(12);
    }

    public function query(){
        $sql = "SELECT `username`,`password`,`address`,`telphone` FROM `user` WHERE `id` IN (4,5)";
        dump(Db::query($sql));
    }
}

